package edu.whu.powersoap;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
  
public class MyVideoActivity extends Activity {  
    /** Called when the activity is first created. */  
    public Intent intent ;
    private String number;
    public Context mContext;
    @Override  
    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_video);  
       	Bundle bundle = getIntent().getExtras(); 
    	number = bundle.getString("number");   	
    	mContext = MyVideoActivity.this;
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);  
        startActivityForResult(intent, 1); 
    }  
   
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        // TODO Auto-generated method stub  
        super.onActivityResult(requestCode, resultCode, data);  
        if (resultCode == Activity.RESULT_OK) {  
            String sdStatus = Environment.getExternalStorageState();  
            if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用  
                Log.i("TestFile",  
                        "SD card is not avaiable/writeable right now.");  
                return;  
            }  

            String fileName = UriToPath(data.getData());
            Toast toast=Toast.makeText(this, "视频已保存在:"+fileName, Toast.LENGTH_LONG); 
            toast.show();           
 
            ContentValues values = new ContentValues();	
            ContentResolver cr = mContext.getContentResolver();          
            Cursor c = cr.query(MarkProvider.CONTENT_URI, new String[] 
					{
					MarkProvider.VOLUMN_NUMBER + " as _id",
					MarkProvider.VOLUMN_VIDEO_PATH, 
					}, "number =?", new String[]{number}, null);
            c.moveToFirst();
			int id = c.getColumnIndex(MarkProvider.VOLUMN_VIDEO_PATH);
			String p_path = c.getString(id);
			c.close();
			String new_path = "";
			if(p_path==null){
				p_path = "";
				new_path = fileName;
			}else{
				new_path =  p_path+" ; " + fileName;
			}

			values.put(MarkProvider.VOLUMN_VIDEO_PATH, new_path);
			values.put(MarkProvider.VOLUMN_FLAG, "marked");
			cr.update(MarkProvider.CONTENT_URI, values, "number =?", new String[]{number});	
			
			MyVideoActivity.this.finish();
        }  
    }  
    
    
	    protected  String UriToPath(Uri uri) {
	        String path = null;
	        ContentResolver cr = mContext.getContentResolver();
	        Cursor cursor = cr.query(uri, null, null, null, null);
	        cursor.moveToFirst();
	        if (cursor != null) {
	            path = cursor.getString(1);
	            cursor.close();
	        }
	        return path;
	    }
    
    
}  
